package nio.file;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
/**
 * 使用 FileChannel将内容写入到文件当中
 * */
public class NIOFileChannel01 {

    public static void main(String[] args) throws IOException {
        FileOutputStream fileOutputStream = new FileOutputStream("G:\\nio\\1.txt");
        FileChannel fileChannel = fileOutputStream.getChannel();
        String context ="hello,nio";
        ByteBuffer byteBuffer = ByteBuffer.allocate(context.getBytes().length);
        //将数据写入到byteBuffer
        byteBuffer.put(context.getBytes());
        //调用flip()后才能从byteBuffer中读取数据
        byteBuffer.flip();
        //把byteBuffer数据写入到fileChannel
        fileChannel.write(byteBuffer);
        fileChannel.close();
        fileOutputStream.close();

    }
}
